package client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
 * Public class. Creates and shows GUI responsible for requesting the server.
 * @author agurgul
 *
 */
public class GUI2Thread implements Runnable {

	@Override
	public void run() {
		createAndShowSecondFrame();
	}
	private void createAndShowSecondFrame(){
		Client.businessFrame = new JFrame("Client");
		Client.businessFrame.setPreferredSize(new Dimension(600, 400));
		Client.businessFrame.setLayout(new GridLayout(1, 2));
		Client.businessFrame.setResizable(false);
		Client.businessFrame.setLocation(100, 100);
		
		Client.buttonsPanel = new JPanel(new GridLayout(5, 1));
		Client.buttonsPanel.setPreferredSize(new Dimension(300, 390));
		
		Client.screenPanel = new JPanel(new FlowLayout());
		
		Client.screen = new JLabel();
		Client.screen.setText("");
		
		Client.inputField = new JTextField();
		Client.inputField.setPreferredSize(new Dimension(300, 75));
		Client.inputField.setText("Give number");
		
		Client.factorisation = new JButton();
		Client.factorisation.setPreferredSize(new Dimension(300, 75));
		Client.factorisation.setText("Get factorisation");
		
		Client.isPrime = new JButton();
		Client.isPrime.setPreferredSize(new Dimension(300, 75));
		Client.isPrime.setText("Check if prime");
		
		Client.lowestDiv = new JButton();
		Client.lowestDiv.setPreferredSize(new Dimension(300, 75));
		Client.lowestDiv.setText("Lowest diversor");
		
		Client.exitButton2 = new JButton();
		Client.exitButton2.setPreferredSize(new Dimension(300, 75));
		Client.exitButton2.setText("Exit");
		
		//LISTENERY------------------------------------
		
		
		Client.exitButton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Client.clientThread.getOutput().println("Q");
				Client.businessFrame.dispose();
				System.exit(2);
			}
		});
		Client.isPrime.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String request = "";
				Client.clientThread.getOutput().println("P "+Client.inputField.getText());
				try{
					request = Client.clientThread.getInput().readLine();
				}catch(IOException e2){
					//e2.printStackTrace();
				}
				if (request.charAt(0) == 'O'){
					if (request.charAt(2) == '1'){
						Client.screen.setText("Number "+Client.inputField.getText()+" is a prime number");
					}
					else{
						Client.screen.setText("Number "+Client.inputField.getText()+" is not a prime number");
					}
				}
				else{
					Client.screen.setText(request.substring(2));
				}
			}
		});
		Client.factorisation.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String request = "";
				Client.clientThread.getOutput().println("R " + Client.inputField.getText());
				try{
					request = Client.clientThread.getInput().readLine();
				}catch(IOException e3){};
				if (request.charAt(0) == 'O'){
					Client.screen.setText(request.substring(2));
				}
				else{
					Client.screen.setText(request.substring(2));
				}
			}
		});
		Client.lowestDiv.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String request = "";
				Client.clientThread.getOutput().println("M " + Client.inputField.getText());
				try{
					request = Client.clientThread.getInput().readLine();
				}catch(IOException e3){};
				if (request.charAt(0) == 'O'){
					Client.screen.setText("The minimal divider of " + Client.inputField.getText()+" is "+request.substring(5, request.length()-1));
				}
				else{
					Client.screen.setText(request.substring(2));
				}				
			}
		});
		
		//KONIEC LISTENEROW---------------------------
		Client.buttonsPanel.add(Client.inputField);
		Client.buttonsPanel.add(Client.factorisation);
		Client.buttonsPanel.add(Client.isPrime);
		Client.buttonsPanel.add(Client.lowestDiv);
		Client.buttonsPanel.add(Client.exitButton2);
		Client.screenPanel.add(Client.screen);
		
		Client.businessFrame.add(Client.buttonsPanel);
		Client.businessFrame.add(Client.screenPanel);
		Client.businessFrame.pack();
		Client.businessFrame.setVisible(true);
	}

}
